package com.namecard.namecardapi.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.entity.TImg;
import com.namecard.namecardapi.entity.vo.TImgQueryVo;
import com.namecard.namecardapi.mapper.TImgMapper;
import com.namecard.namecardapi.service.TImgService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xb
 * @since 2024-04-21
 */
@Service
public class TImgServiceImpl extends ServiceImpl<TImgMapper, TImg> implements TImgService {

    @Resource
    private TImgMapper imgMapper;

    /**
     * 文件上传
     * @param file
     * @param path
     * @return
     * @throws IOException
     */
    @Override
    public String uploadDoc(MultipartFile file, String path) throws IOException {
        //文件的名称
        String originalFilename = file.getOriginalFilename();
        //文件类型
        String type = FileUtil.extName(originalFilename);
        //文件大小
        long size = file.getSize();
        //获取文件
        File uploadFile = new File(path);
        //判断磁盘文件是否存在
        if (!uploadFile.exists()) {
            uploadFile.mkdirs();//创建磁盘文件
        }
        //文件唯一的标识码
        String uid = IdUtil.fastSimpleUUID();
        String fileUid = uid + StrUtil.DOT + type;
        //获取上传文件
        File fullFile = new File(path + fileUid);//上传文件的完整路径
        //获取文件的md5
        String md5 = SecureUtil.md5(file.getInputStream());
        //MD5查询数据库是否存在该文件
        TImg img = getFileByMd5(md5);
        String url = null;
        if (img != null) {//文件存在
            url = img.getImgUrl();
        } else {//文件不存在
            //上传到磁盘
            file.transferTo(fullFile);
            //http://localhost:8800/img?fileUid=
            url = "/admin/system/t-img/download?fileUid=" + fileUid;
        }
        // 存储数据库
        TImg tImg = new TImg();
        tImg.setImgName(originalFilename);
        tImg.setImgType(type);
        tImg.setImgSize(size / 1024);
        tImg.setImgUrl(url);
        tImg.setMd5(md5);
        imgMapper.insert(tImg);
        return url;
    }

    //文件分页查询
    @Override
    public IPage<TImg> selectPage(Page<TImg> pageParam, TImgQueryVo tImgQueryVo) {
        IPage<TImg> pageModel = baseMapper.selectPage(pageParam, tImgQueryVo);
        return pageModel;
    }

    //根据MD5查询文件
    private TImg getFileByMd5(String md5) {
        // 查询文件的md5是否存在
        QueryWrapper<TImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("md5", md5);
        List<TImg> imgs = imgMapper.selectList(queryWrapper);
        return imgs.size() == 0 ? null : imgs.get(0);
    }


}
